package com.hzsoft.login.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hzsoft.sys.entity.SysMenu;
import com.hzsoft.sys.entity.SysUser;
import com.hzsoft.sys.service.SysMenuService;
import com.hzsoft.sys.service.SysUserService;
import com.hzsoft.util.Const;
import com.hzsoft.util.SystemUtil;

@Controller
public class LoginController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired 
	private SysMenuService sysMenuService;
	/**
	 * 访问登录页
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String loginGet(){
		return "login";
	}
	 
	/**
	 * 请求登录，验证用户
	 * @param session
	 * @param loginname
	 * @param password
	 * @param code
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ModelAndView loginin(HttpSession session,@RequestParam String username,@RequestParam String password){
		ModelAndView mv = new ModelAndView();
		String errInfo = "";
		//根据用户名从表sysUser中取出用户记录user
		SysUser user = sysUserService.selectByUsername(username);
		if (user != null) {//判断该用户是否已注册并且未离职
			if(StringUtils.isNotEmpty(password)){
				//登录页面的密码经过md5加密后
				String pass = SystemUtil.getMD5Code(password+user.getSalt().trim());
				if(null!=pass&&pass.equals(user.getPassword())){
					session.setAttribute(Const.SESSION_USER, user);
				}else{
					errInfo = "用户名或密码有误！";
				}
			}else{
				if(StringUtils.isEmpty(user.getPassword())){
					session.setAttribute(Const.SESSION_USER, user);
				}else{
					errInfo = "用户名或密码有误！";
				}
			}
		} else {
			errInfo = "用户名或密码有误！";
		}
		if (StringUtils.isEmpty(errInfo)) {
//			user.setLoginFlag("1");
//			dcTblRegUserService.setLoginFlag(user);
			mv.setViewName("redirect:index.do");
		} else {
			mv.addObject("errInfo", errInfo);
			mv.addObject("username", username);
			mv.setViewName("/login");
		}
		return mv;
	}
	
	/**
	 * 访问系统首页
	 * @param session
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/index")
	public ModelAndView index(HttpSession session,Model model){
		ModelAndView mv = new ModelAndView();
		
		SysUser userinfo=(SysUser) session.getAttribute(Const.SESSION_USER);
		String userId = userinfo.getUserId();
		
		List<SysMenu> menulist = sysMenuService.listMenus(userId);
		
		session.setAttribute("menulist", menulist);
		mv.addObject("menulist", menulist);
		mv.setViewName("index");
		return mv;
		
	}
	
	/**
	 * 用户注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		Object obj = session.getAttribute(Const.SESSION_USER);
		if(null != obj){
			SysUser user = (SysUser)obj;
//			user.setLoginFlag("0");
//			dcTblRegUserService.setLoginFlag(user);
		}
		session.removeAttribute("menulist");
		session.removeAttribute(Const.SESSION_USER);
		return "redirect:login.do";
	}
}
